<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="${mapperInterfaceModel.packageNamePrefix}.${mapperInterfaceModel.className}">
    <insert id="saveEntity" <#if entityModel.pkField??> useGeneratedKeys="true" keyProperty="${entityModel.pkField.fieldName}"</#if>>
        INSERT INTO  ${entityModel.tableName}(
            <include refid="_columns"/>
        ) VALUES (
         <#assign commaIndex=0/>  
         <#list entityModel.fields as f>
            <#if entityModel.pkField??&&f_index==0&&entityModel.pkField.fieldName==f.fieldName>
                <#assign commaIndex=1/>
            </#if>
            <#if !entityModel.pkField??||entityModel.pkField.fieldName!=f.fieldName><#if f_index!=commaIndex>,</#if>${r'#{'}${f.fieldName}${r'}'}</#if>
         </#list>
        )
    </insert>
    
    <!-- public List<T> findListByCriteria(Map<String, Object> param); -->
    <select id="findListByCriteria" resultMap="BaseResultMap" >
        SELECT <include refid="t_full_columns" />
        FROM ${entityModel.tableName}  T 
        <include refid="findPage_where" />
    </select>
    
    <!-- 根据条件对象查询数量 -->
    <select id="findCountByCriteria" resultType="int" parameterType="java.util.Map">
        SELECT COUNT(*) FROM ${entityModel.tableName} 
        <include refid="findPage_where" />
    </select>
    
    
    <sql id="findPage_where">
        <where>
        </where>
    </sql>

    <#if entityModel.pkField??>
    
    <!-- 更新 -->
    <update id="updateEntity" parameterType="${entityModel.packageNamePrefix}.${entityModel.className}">
        UPDATE ${entityModel.tableName}
        <set>
        <#list entityModel.fields as f>
        <#if !entityModel.pkField??||entityModel.pkField.fieldName!=f.fieldName><if test="@Ognl@isNotEmpty(${f.fieldName})">${f.columnName}=${r'#{'}${f.fieldName}${r'}'},</if></#if>
        </#list>
        </set>
        WHERE ${entityModel.pkField.columnName} = ${r'#{'}${entityModel.pkField.fieldName}${r'}'}
    </update>

    <!-- 删除 -->
    <delete id="deleteEntity" parameterType="java.lang.Long">
        DELETE FROM ${entityModel.tableName}  WHERE ${entityModel.pkField.columnName} = ${r'#{'}${entityModel.pkField.fieldName}${r'}'}
    </delete>
    
    <!--  public int deleteByPKArray(Serializable[] ids); -->
    <delete id="deleteByPKArray" >
        DELETE FROM ${entityModel.tableName}  
        WHERE ${entityModel.pkField.columnName} IN 
        (<foreach collection="array" separator="," item="id"> ${r'#{'}id${r'}'}</foreach>)
    </delete>

    <select id="findByPK" resultMap="BaseResultMap">
        SELECT <include refid="t_full_columns" />
        FROM ${entityModel.tableName}  T 
        WHERE ${entityModel.pkField.columnName} = ${r'#{'}${entityModel.pkField.fieldName}${r'}'}
    </select>
    
    <!-- 根据条件对象进行分页查询，查询结果放在BaseResultMap中 -->
    <select id="findPageListByCriteria" resultMap="BaseResultMap" >
        SELECT <include refid="t_full_columns" />
        FROM ${entityModel.tableName}  T 
        <include refid="findPage_where" />
        ORDER BY T.${entityModel.pkField.columnName} 
    </select>
    </#if>
    
    <resultMap type="${entityModel.packageNamePrefix}.${entityModel.className}" id="BaseResultMap">
        <#list entityModel.fields as f>
            <result property="${f.fieldName}" column="${f.columnName}" />
        </#list>
    </resultMap>

    <sql id="t_full_columns">
        <#list entityModel.fields as f>
            <#if f_index!=0>,</#if>T.`${f.columnName}`
         </#list>
    </sql>  
    
    <#if entityModel.pkField??>
    <sql id="full_columns">
        <#list entityModel.fields as f>
            <#if f_index!=0>,</#if>`${f.columnName}`
         </#list>
    </sql>
    </#if>
    
    <sql id="_columns">
        <#assign commaIndex=0/>  
        <#list entityModel.fields as f>
            <#if entityModel.pkField??&&f_index==0&&entityModel.pkField.fieldName==f.fieldName>
                <#assign commaIndex=1/>
            </#if>
            <#if !entityModel.pkField??||entityModel.pkField.fieldName!=f.fieldName><#if f_index!=commaIndex>,</#if>`${f.columnName}`</#if>
         </#list>
    </sql>
</mapper>